using System;
using System.Web;
using SoftwareMonkeys.Scriptech.Diagnostics;

namespace SoftwareMonkeys.Scriptech.Web.Projections
{
	public class ProjectionHttpHandler : IHttpHandler
	{
		public bool IsReusable {
			get { return false; }
		}

		private string virtualPath;

		public ProjectionHttpHandler (string virtualPath)
		{
			this.virtualPath = virtualPath;
						
		}

		public void ProcessRequest (HttpContext context)
		{
			using (LogGroup logGroup = LogGroup.StartDebug("Processing request via projection HTTP handler."))
			{				
				string internalUrl = GetInternalUrl(virtualPath);
				
				// If an internal URL was generated then redirect (if not then skip it)
				if (internalUrl != String.Empty)
					Redirector.Go(internalUrl);
			}
		}
		
		public string GetInternalUrl(string friendlyUrl)
		{
			ProjectionMapper mapper = new ProjectionMapper();
			return mapper.GetInternalPath(friendlyUrl);
		}
	}
}

